package kr.or.kead.ui.meun;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import kr.or.kead.domain.Auth;
import kr.or.kead.domain.Course;
import kr.or.kead.domain.Professor;
import kr.or.kead.service.DaoCourse;
import kr.or.kead.service.DaoProfessor;
import kr.or.kead.ui.insert_update.CourseInsertUpdate;
import kr.or.kead.ui.list.AbsTableList;
import kr.or.kead.ui.list.CourseTableList;

public class CourseMenu extends AbsMeun {
	private AbsTableList courseListView;
	private DaoCourse daoCourse;
	private DaoProfessor daoProf;
	private Auth auth;
	
	public CourseMenu(JFrame frame, int height) {
		super(frame, "깅좌관리");
		
		daoProf = new DaoProfessor();
		courseListView = new CourseTableList(height);
		daoCourse = new DaoCourse();
	}

	@Override
	protected void addMenuActionPerformed(ActionEvent e) {
		listMenuActionPerformed(e);
		CourseInsertUpdate insert = new CourseInsertUpdate(null);
		insert.setVisible(true);
		if (insert.showDialog()==0)courseListView.setTableModel();
	}

	@Override
	protected void delMenuActionPerformed(ActionEvent e) {
		listMenuActionPerformed(e);
		auth = MenuMgn.getAuth();
		int level = auth.getLevel();
		Professor prof;
		int res = 0;
		if(level == 2) {
			prof = (Professor)daoProf.selectTableByEmail(auth.getEmail());
			res=searchNum(daoCourse, "삭제", prof.getCode());
		}else {
			res=searchNum(daoCourse, "삭제", 0);
		}		
		if (res != -1 && daoCourse.deleteDao(res)!=-1){
			courseListView.setTableModel();
			JOptionPane.showMessageDialog(null, "삭제되었습니다.");
		}

	}

	@Override
	protected void updateMenuActionPerformed(ActionEvent e) {
		listMenuActionPerformed(e);
		auth = MenuMgn.getAuth();
		int level = auth.getLevel();
		Professor prof;
		int res = 0;
		if(level == 2) {
			prof = (Professor)daoProf.selectTableByEmail(auth.getEmail());
			res=searchNum(daoCourse, "수정", prof.getCode());
		}else {
			res=searchNum(daoCourse, "수정", 0);
		}
		if (res != -1){
			Course depart = (Course)daoCourse.selectTableById(res);
			CourseInsertUpdate update = new CourseInsertUpdate(depart);
			update.setVisible(true);
			if (update.showDialog()==0)courseListView.setTableModel();
		}

	}

	@Override
	protected void listMenuActionPerformed(ActionEvent e) {
		refreshList(courseListView);

	}

}
